package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.LotInfoBindAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class GrindingStockActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etStorageid)
    FreeUI_ClearEditText etStorageid;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.flPositionList)
    FrameLayout flPositionList;
    @BindView(R.id.ivPositionArrow)
    ImageView ivPositionArrow;
    @BindView(R.id.rvLotList)
    RecyclerView rvLotList;
    private List<String> mMaterielList;
    //批次数量
    private FreeUI_GeneralPop codePop;
    private String defectCode = "";
    private int number = 0;
    private LotInfoBindAdapter mLotInfoBindAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_grindingstock;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText(String.valueOf(number));
        mMaterielList = new ArrayList<>();
        mMaterielList.add("04001/一分厂线边仓");
        mMaterielList.add("04002/二分厂线边仓");
        mMaterielList.add("04003/三分厂线边仓");
        tvEqpType.setText(mMaterielList.get(0));
        defectCode = mMaterielList.get(0).split("/")[0];
        initRecyclerView(rvLotList, this);
        mLotInfoBindAdapter = new LotInfoBindAdapter(null);
        mLotInfoBindAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mLotInfoBindAdapter.isFirstOnly(true);
        rvLotList.setAdapter(mLotInfoBindAdapter);
    }


    @Override
    protected void initData() {

        initEdit(etLot, s -> {

            FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                    ("正在查询LOT信息", new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(GrindingStockActivity.this, true, getSupportFragmentManager())) {
                        mLotInfoBindAdapter.addData(0,response.body().getResponse().getBody().getLOTLIST());
                        etLot.requestFocus();
                        int integer = Integer.parseInt(tvBaseSubmit.getText().toString());
                        tvBaseSubmit.setText(String.valueOf(integer + 1));
                    }

                }

                @Override
                public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(), "LOTMHSHIP");
                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("LOTID",s);
            map.addProperty("WAREHOUSEID",defectCode);
            map.addProperty("STORAGEID",getText(etStorageid));
            etLot.setText("");
            etLot.requestFocus();
            NetUtils.requestPostNet("IF.LOTMHSHIP", "Common", GrindingStockActivity.this, map, callBack);

        });

    }

    @OnClick({R.id.flPositionList})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.flPositionList:
                codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flPositionList.getWidth(), mMaterielList, ivPositionArrow, (codeS, i) -> {
                    tvEqpType.setText(codeS);
                    defectCode = codeS.split("/")[0];
                });
                FreeApi_PopUtils.setArrowShow(ivPositionArrow);
                codePop.showAsDropDown(flPositionList, 0, 0);
                break;
        }

    }
}